const express = require('express')
const Product = require('../models/product.model')

const router = express.Router()

router.get('/products', async (req, res) =>{
    try{
	const products = await Product.find({});
	res.status(200).json(products);
    }catch(err){
	res.status(500).json({message: err.message});
    }
})

router.get('/products/:id', async (req, res) =>{
    try{
	const {id} = req.params;
	const product = await Product.findById(id);
	res.status(200).json(product)
    }catch(err){
	res.status(500).json({message: err.message});
    }
})

//add new comment
router.put('/product/:id', async (req, res) =>{
    try{
	const {id} = req.params;
	const product = await Product.findByIdAndUpdate(id, req.body);
	if(!product){
	    return res.send(404).json({message: "comment not posted"});
	}
	const updateProduct = await Product.findById(id);
	res.status(200).json(updateProduct);
    }catch(err){
	res.status(500).json({message: err.message})
    }
})

module.exports = router
